package com.zhitong.money.mapper;

import com.zhitong.money.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {

    @Results(id = "userMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "tid",property = "tid"),
            @Result(column = "userName",property = "userName"),
            @Result(column = "password",property = "password"),
            @Result(column = "nickName",property = "nickName"),
            @Result(column = "iconUrl",property = "iconUrl"),
            @Result(column = "createTime",property = "createTime"),
            @Result(column = "updateTime",property = "updateTime"),
            @Result(column = "lastTime",property = "lastTime"),
            @Result(column = "status",property = "status"),
            @Result(property = "roles",column = "id",
                    many = @Many(select = "com.zhitong.money.mapper.RoleMapper.findRoleByUid",fetchType = FetchType.EAGER))
    })
    @Select("select * from User where userName=#{userName}")
    public User findUserByUserName(String userName);

    @Select({
            "<script>",
            "select id from user",
            "where tid in",
            "<foreach item='item' collection='tids' separator=',' open='(' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public List<String> findIdsByTids(List<String> tids);

    @ResultMap("userMap")
    @Select("select * from user where tid=#{tid}")
    public User findUserByTid(String tid);

    @ResultMap("userMap")
    @Select("select * from user where id=#{id}")
    public User findUserByid(String id);

    @ResultMap("userMap")
    @Select("select * from user")
    public List<User> findAllUser();

    @ResultMap("userMap")
    @Select({
            "<script>",
            "select * from user ",
            "<where>",
            "<if test='userName!=null and userName !=\"\"'>and userName like concat(concat('%',#{userName},'%'))</if>",
            "<if test='tid!=null and tid !=\"\"'>and tid=#{tid}</if>",
            "<if test='nickName!=null and nickName !=\"\"'>and nickName like concat(concat('%',#{nickName},'%'))</if>",
            "<if test='status!=null and status !=\"\"'>and status=#{status}</if>",
            "</where>",
            "</script>"
    })
    public List<User> findUserByMap(Map<String,Object> map);

    @Insert("insert into user values(#{id},#{tid},#{userName},#{password},#{nickName}," +
            "#{iconUrl},NOW(),null,null,#{status})")
    public void addUser(Map<String,Object> map);

    /**
     * 客户修改信息
     * @param map
     */
    @Update({
            "<script>",
            "update user",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='tid!=null and tid!=\"\"'> tid=#{tid},</if>",
            "<if test='userName!=null and userName!=\"\"'> userName=#{userName},</if>",
            "<if test='password!=null and password!=\"\"'> password=#{password},</if>",
            "<if test='nickName!=null and nickName!=\"\"'> nickName=#{nickName},</if>",
            "<if test='iconUrl!=null and iconUrl!=\"\"'>iconUrl=#{iconUrl},</if>",
            "<if test='createTime!=null'> createTime=#{createTime},</if>",
            "<if test='updateTime!=null'> updateTime=#{updateTime},</if>",
            "<if test='lastTime!=null'> lastTime=#{lastTime},</if>",
            "<if test='status!=null and status!=\"\"'> status=#{status},</if>",
            "</trim>",
            "where id = #{id}",
            "</script>"
    })
    public void updateUser(Map<String,Object> map);

    @Update("update user set lastTime = #{lastTime} where id = #{id}")
    public void updateLastTime(Map<String,Object> map);

    @Delete("delete from User where id=#{id}")
    public void delUser(String id);

    @Delete({
            "<script>",
            "delete from user",
            "where id in",
            "<foreach item='item' collection='ids' separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void delUserByIds(List<String> ids);

    @Delete({
            "<script>",
            "delete from user",
            "where tid in",
            "<foreach item='item' collection='tids' separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void delUserByTids(List<String> tids);

    @Delete("delete from user where tid=#{tid}")
    public void delUserByTid(String tid);


}
